﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Common.Servers
{
    public class DocumentServers
    {
        private readonly IRepository<official_document> officialDocumentRepository;

        public DocumentServers()
        {
            officialDocumentRepository = new DapperRepository<official_document>();
        }

        public IEnumerable<official_document> GetBorrowList()
        {
            string sql = "SELECT * FROM [OADB].[dbo].[official_document] WHERE [STATUS]= 10";
            return officialDocumentRepository.SqlWithParams(sql);
        }

        public List<official_document> GetData(string userNO,string SUBJECT, string DOC_NUM, string M_START, string M_END)
        {
            string sql = string.Format("SELECT * FROM [OADB].[dbo].[official_document] WHERE [DRAFT_AUTHOR]='{0}' OR [DEPT_LEADER_USER_NO]='{0}' OR [SIGNER_USER_NO]='{0}'", userNO);            
            var datas = officialDocumentRepository.SqlWithParams(sql);
            if (!string.IsNullOrEmpty(SUBJECT))
            {
                datas = datas.Where(a => a.SUBJECT.Contains(SUBJECT));
            }
            if (!string.IsNullOrEmpty(DOC_NUM))
            {
                datas = datas.Where(a => a.DOC_NUM.Contains(DOC_NUM));
            }
            if (!string.IsNullOrEmpty(M_START))
            {
                datas = datas.Where(a => a.SIGNATURE_TIME >= Convert.ToDateTime(M_START));
            }
            if (!string.IsNullOrEmpty(M_END))
            {
                datas = datas.Where(a => a.SIGNATURE_TIME < Convert.ToDateTime(M_END));
            }
            return datas.ToList();
        }

        public official_document GetModelById(int id)
        {
            return officialDocumentRepository.GetById(id);
        }


        internal void Add(official_document model)
        {
            officialDocumentRepository.Insert(model);
        }

        internal void Update(official_document model)
        {
            officialDocumentRepository.Update(model);
        }

        internal void UpdateStatus(int id, int status)
        {
            string sql = string.Format("UPDATE [dbo].[official_document] SET [STATUS] = '{0}' WHERE [DOC_ID] = {1}", status, id);
            officialDocumentRepository.SqlWithParams(sql);
        }

        internal void UpdateArchiveStatus(int id)
        {
            string sql = string.Format("UPDATE [dbo].[official_document] SET [IS_ARCHIVE] = '1' WHERE [DOC_ID] = {0}", id);
            officialDocumentRepository.SqlWithParams(sql);
        }

        internal void UpdateStatus(int id, int status, string remark)
        {
            string sql = string.Format("UPDATE [dbo].[official_document] SET [STATUS] = '{0}',[REMARK]='{1}' WHERE [DOC_ID] = {2}", status, remark, id);
            officialDocumentRepository.SqlWithParams(sql);
        }

        internal void UpdateSigner(int id, string signerNO, string signerName)
        {
            string sql = string.Format("UPDATE [dbo].[official_document] SET [SIGNER_USER_NO] = '{0}',[SIGNER]='{1}' WHERE [DOC_ID] = {2}", signerNO, signerName, id);
            officialDocumentRepository.SqlWithParams(sql);
        }

        internal void Del(string id)
        {
            officialDocumentRepository.DeleteByIds(id);
        }

    }
}